package com.ipzoe.act.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ipzoe.act.domain.ActivitySignup;
import com.ipzoe.act.model.SignedActivityBO;
import com.ipzoe.act.service.IActivitySignupService;
import com.ipzoe.common.exception.CustomException;
import com.ipzoe.mem.domain.Member;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.Banner;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ipzoe.act.mapper.ActivityMapper;
import com.ipzoe.act.domain.Activity;
import com.ipzoe.act.service.IActivityService;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * 活动Service业务层处理
 *
 * @author eric
 * @date 2020-08-17
 */
@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> implements IActivityService {

    @Autowired
    private IActivitySignupService iActivitySignupService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByIds(Collection<? extends Serializable> idList, String updateBy) {

        boolean result = update(Wrappers.<Activity>lambdaUpdate().set(Activity::getUpdateBy, updateBy)
                .set(Activity::getUpdateTime,new Date())
                .in(Activity::getId,idList));

        return result && removeByIds(idList);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void signUp(Long activityId, String name, String nickname, String phone, String email, Member member) {
        if(activityId == null){
            throw new CustomException(400, "请针对具体的活动进行报名");
        }
        if(member == null){
            throw new CustomException(400, "缺少会员信息");
        }
        if(StrUtil.isBlank(name)){
            throw new CustomException(400, "请输入姓名");
        }
        if(StrUtil.isEmpty(phone)){
            throw new CustomException(400,"请输入手机号码");
        }
        if(!Validator.isMobile(phone)){
            throw new CustomException(400,"请输入正确的手机号码");
        }
        if(StrUtil.isEmpty(email)){
            throw new CustomException(400,"请输入邮箱");
        }
        if(!Validator.isEmail(email)){
            throw new CustomException(400,"请输入正确的邮箱");
        }
        Date today = new Date();
        Activity activity = getById(activityId);
        if(activity==null){
            throw new CustomException(400,"未找到相关的活动");
        }
        if(2 == activity.getPermission()){
            //如果是会员专享的活动
            if(member.getMemberLevel() != 2){
                throw new CustomException(400,"活动为会员专享，非有效会员不能报名");
            }
        }
        Date beginOfDayBeginTime = DateUtil.beginOfDay(activity.getBeginTime());
        Date beginOfDayEndTime = DateUtil.beginOfDay(activity.getEndTime());
        Date beginOfDayToday = DateUtil.beginOfDay(today);
        if(activity.getStatus()==1 || !beginOfDayToday.before(beginOfDayBeginTime)){
            throw new CustomException(400,"活动已经开始，已不可报名该活动");
        }
        if(activity.getStatus()==2 || beginOfDayToday.after(beginOfDayEndTime)){
            throw new CustomException(400,"活动已结束，不可报名");
        }

        activity.setSignupNum(activity.getSignupNum()+1);
        updateById(activity);

        //查询是否已经报名
        LambdaQueryWrapper<ActivitySignup> activitySignupLqw = new LambdaQueryWrapper<>();
        activitySignupLqw.eq(ActivitySignup::getActivityId, activityId).eq(ActivitySignup::getMobile, phone);
        ActivitySignup activitySignup = iActivitySignupService.getOne(activitySignupLqw);
        if(activitySignup !=  null){
            throw new CustomException(400,"该手机号码已经报名该活动，不可再次报名");
        }

        activitySignup = new ActivitySignup();
        activitySignup.setStatus(0)
                .setActivityId(activityId)
                .setEmail(email)
                .setMemberId(member.getId())
                .setMobile(phone)
                .setNickname(nickname)
                .setUsername(name)
        ;
        iActivitySignupService.save(activitySignup);
    }

    @Override
    public List<SignedActivityBO> listSignedActivity(Long memberId, Integer signStatus, Integer activityStatus) {
        return baseMapper.listSignedActivity(memberId,signStatus,activityStatus);
    }
}
